package com.mesnac.comm.sysinfo;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.mesnac.comm.util.XMLTools;
/**
 * 
* @ClassName: SysInfo
* @Description: 这个类用于存储系统的配置信息
* @author chilei chil07@mesnac.com
* @date Sep 5, 2012 11:19:18 AM
*
 */
public class SysInfo {

	public static Log logger = LogFactory.getLog(SysInfo.class);
	public static String sysname = "";
	public static String company = "";
	public static boolean isUploadAble = false;
	public static long maxFileSize = 524288000;
	public static long totalMaxFileSize = 2097152000;
	public static String path = "";
	public static String filePath = "";
	public static String imagePath = "";
	public static int pageSize = 10;
	public static String numberScale = "";
	public static int loopTimes = 3;
	public static int turns = 1;
	public static int leaderTurn = 1;
	public static String rootpath = "D:\\pinshuo";
	public static String tomcatwebapp = "D:\\omcat6.0\\webapps\\pinshuoinfor";
	
	private static XMLTools xmlTools = null;
	private static String sysInfoPath = null;
	
	public XMLTools getXmlTools() {
		return xmlTools;
	}
	public void setXmlTools(XMLTools xmlTools) {
		this.xmlTools = xmlTools;
	}
	public static int getTurns() {
		return turns;
	}
	public static void setTurns(int turns) {
		SysInfo.turns = turns;
	}
	public static int getLeaderTurn() {
		return leaderTurn;
	}
	public static void setLeaderTurn(int leaderTurn) {
		SysInfo.leaderTurn = leaderTurn;
	}
	public static Log getLogger() {
		return logger;
	}
	public static void setLogger(Log logger) {
		SysInfo.logger = logger;
	}
	public static String getSysname() {
		return sysname;
	}
	public static void setSysname(String sysname) {
		SysInfo.sysname = sysname;
	}
	public static String getCompany() {
		return company;
	}
	public static void setCompany(String company) {
		SysInfo.company = company;
	}
	public static boolean isUploadAble() {
		return isUploadAble;
	}
	public static void setUploadAble(boolean isUploadAble) {
		SysInfo.isUploadAble = isUploadAble;
	}
	public static long getMaxFileSize() {
		return maxFileSize;
	}
	public static void setMaxFileSize(long maxFileSize) {
		SysInfo.maxFileSize = maxFileSize;
	}
	public static long getTotalMaxFileSize() {
		return totalMaxFileSize;
	}
	public static void setTotalMaxFileSize(long totalMaxFileSize) {
		SysInfo.totalMaxFileSize = totalMaxFileSize;
	}
	public static String getPath() {
		return path;
	}
	public static void setPath(String path) {
		SysInfo.path = path;
	}
	public static String getFilePath() {
		return filePath;
	}
	public static void setFilePath(String filePath) {
		SysInfo.filePath = filePath;
	}
	public static String getImagePath() {
		return imagePath;
	}
	public static void setImagePath(String imagePath) {
		SysInfo.imagePath = imagePath;
	}
	public static int getPageSize() {
		return pageSize;
	}
	public static void setPageSize(int pageSize) {
		SysInfo.pageSize = pageSize;
	}
	/**
	 * 
	* @Title: init
	* @Description: 将配置文件中的信息读取到SysInfo类中
	* @param @param servletContext
	* @param @param servletConfig
	* @param @throws Exception    设定文件
	* @return void    返回类型
	* @throws
	 */
	public static void init(ServletContext servletContext,ServletConfig servletConfig) throws Exception{
		
		if(servletConfig.getInitParameter("sysInfo")==null||servletConfig.getInitParameter("sysInfo").equals("")){
			throw new RuntimeException("sysInfo.xml file not found");
		}
		sysInfoPath = servletContext.getRealPath(servletConfig.getInitParameter("sysInfo"));
		System.out.println(sysInfoPath);
		try{
			xmlTools = new XMLTools(sysInfoPath);
		}catch (FileNotFoundException e) {
			logger.debug(e);
			e.printStackTrace();
		}catch(DocumentException e){
			logger.debug(e);
			e.printStackTrace();
		}
		List paramList = new ArrayList();
		String s = null;
		Element sysInfo = xmlTools.getRoot().element("sysInfo");
		if(sysInfo.elements("param")!=null){
			paramList = sysInfo.elements("param");
			
		}
		if((s = getStrFromElement(paramList, "sysname"))!=null){
			sysname = getStrFromElement(paramList, "sysname");
		}
		if((s = getStrFromElement(paramList, "company"))!=null){
			company = getStrFromElement(paramList, "company");
		}
		if((s = getStrFromElement(paramList, "isUploadAble"))!=null){
			isUploadAble = (Boolean.parseBoolean(s));
		}
		if((s = getStrFromElement(paramList, "totalMaxFileSize"))!=null){
			totalMaxFileSize = Long.parseLong(s);
		}
		if((s = getStrFromElement(paramList, "path"))!=null){
			path = s;
		}
		if((s = getStrFromElement(paramList, "filePath"))!=null){
			filePath = s;
		}
		if((s = getStrFromElement(paramList, "imagePath"))!=null){
			imagePath = s;
		}
		if((s = getStrFromElement(paramList, "pageSize"))!=null){
			pageSize = Integer.parseInt(s);
		}
		if((s = getStrFromElement(paramList, "numberScale"))!=null){
			numberScale = s;
		}
		if((s = getStrFromElement(paramList, "rootpath"))!=null){
			rootpath = s;
		}
		if((s = getStrFromElement(paramList, "tomcatwebapp"))!=null){
			tomcatwebapp = s;
		}
		List sysParamsList = new ArrayList();
		Element sysParam = xmlTools.getRoot().element("sysParams");
		Element selectProcessElement = sysParam.element("selectProcess");
		if(selectProcessElement.elements("param")!=null){
			sysParamsList = selectProcessElement.elements("param");
		}
		if((s = getStrFromElement(sysParamsList, "loopTimes"))!=null){
			loopTimes = Integer.parseInt(s);
		}
		if((s = getStrFromElement(sysParamsList, "turns"))!=null){
			turns = Integer.parseInt(s);
		}
		if((s = getStrFromElement(sysParamsList, "leaderTurn"))!=null){
			leaderTurn = Integer.parseInt(s);
		}
	}
	private static String getStrFromElement(List paramList,String param){
		if(paramList==null)
			return null;
		for(int i = 0; i < paramList.size(); i++){
			Element tmp = (Element)paramList.get(i);
			if(tmp.attributeValue("key").trim().toLowerCase().equals(param.trim().toLowerCase())){
				System.out.println(param+"=>"+tmp.getTextTrim());
				return tmp.getTextTrim();
			}
			tmp = null;
		}
		return null;
		
	}
	private static Element getElement(List paramList,String key){
		if(paramList == null)
			return null;
		Iterator it = paramList.iterator();
		while(it.hasNext()){
			Element tmp = (Element)it.next();
			if(tmp.attributeValue("key").trim().toLowerCase().equals(key.trim().toLowerCase())){
				return tmp;
			}
		}
		return null;
	}
	public static void changeSysStr(String paramName,Object value){
		try{
			OutputFormat format = OutputFormat.createPrettyPrint();
			//format.setEncoding("UTF-8");
			//乱码
			//XMLWriter writer = new XMLWriter(new FileWriter(sysInfoPath),format);
			FileOutputStream fos = new FileOutputStream(sysInfoPath);
			XMLWriter writer = new XMLWriter(fos);
			List sysParamsList = new ArrayList();
			Element sysParam = xmlTools.getRoot().element("sysParams");
			
			Element selectProcessElement = sysParam.element("selectProcess");
			if(selectProcessElement.elements("param")!=null)
				sysParamsList = selectProcessElement.elements("param");
			Element itElement = getElement(sysParamsList, paramName);
			itElement.setText(value.toString());
			System.out.println(itElement.getTextTrim()+"changebefore");
			writer.write(xmlTools.getDoc());
			writer.close();
			System.out.println("change!");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	public static int getLoopTimes() {
		return loopTimes;
	}
	public static void setLoopTimes(int loopTimes) {
		SysInfo.loopTimes = loopTimes;
	}
	public static String getNumberScale() {
		return numberScale;
	}
	public static void setNumberScale(String numberScale) {
		SysInfo.numberScale = numberScale;
	}
	public static String getSysInfoPath() {
		return sysInfoPath;
	}
	public static void setSysInfoPath(String sysInfoPath) {
		SysInfo.sysInfoPath = sysInfoPath;
	}
	public static String getRootpath() {
		return rootpath;
	}
	public static void setRootpath(String rootpath) {
		SysInfo.rootpath = rootpath;
	}
	public static String getTomcatwebapp() {
		return tomcatwebapp;
	}
	public static void setTomcatwebapp(String tomcatwebapp) {
		SysInfo.tomcatwebapp = tomcatwebapp;
	}
}
